package com.library.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.library.management.common.PageResult;
import com.library.management.entity.BookComment;
import com.library.management.mapper.BookCommentMapper;
import com.library.management.service.BookCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 图书评论服务实现类
 */
@Service
public class BookCommentServiceImpl implements BookCommentService {
    @Autowired
    private BookCommentMapper bookCommentMapper;

    @Override
    public int add(BookComment bookComment) {
        return bookCommentMapper.insert(bookComment);
    }

    @Override
    public int delete(Long id) {
        return bookCommentMapper.deleteById(id);
    }

    @Override
    public int update(BookComment bookComment) {
        return bookCommentMapper.update(bookComment);
    }

    @Override
    public BookComment getById(Long id) {
        return bookCommentMapper.selectById(id);
    }

    @Override
    public PageResult<BookComment> list(BookComment bookComment, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BookComment> list = bookCommentMapper.selectList(bookComment);
        PageInfo<BookComment> pageInfo = new PageInfo<>(list);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<BookComment> listByUserId(Long userId) {
        return bookCommentMapper.selectByUserId(userId);
    }

    @Override
    public List<BookComment> listByBookId(Long bookId) {
        return bookCommentMapper.selectByBookId(bookId);
    }
}